package MapDemo.TreeMapdemo.demo2;

import java.util.TreeMap;
import java.util.function.BiConsumer;

public class test {
    public static void main(String[] args){
        // 定义字符串
        String str = "abcdebaecccabccdd";
        // 定义集合存储数据
        TreeMap<Character,Integer> tm = new TreeMap<>();
        // 遍历字符串获取每一个字符
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            // 如果存在则次数加一
            if (tm.containsKey(c)){
                int count = tm.get(c);
                count++;
                tm.put(c,count);
            }else{
                // 不存在则创建
                tm.put(c,1);
            }
        }
        
        StringBuilder sb = new StringBuilder();
        // 拼接字符串
        tm.forEach((Character key,Integer value) -> sb.append(key).append("(").append(value).append(")"));
        System.out.println(sb);
    }
}
